package DBase;

import DBase.Ships.Ship;

/**
 * 
 * @author Dana & Elya
 */
public class Board {
    
    private Cell[][]gameBoard = new Cell[10][10];
    private int numOfShipsActive;    
    private int numOfShipsNotActive;
    private int PlayerNumber;
    private Ship [] ships;
    private boolean isBoardActive;
    /**
     * Board Constructor
     */
    public Board(){
        
        ships= new Ship[5];
        numOfShipsActive=5;
        numOfShipsNotActive=0;
        isBoardActive=true;
        
        for(int i=0; i<10; i++)
            for(int j=0; j<10; j++)
               gameBoard[i][j] = new Cell(i,j);
        
    }
    
    
    /**
     * This function restarts a game
     */
    public void restart(){
        
        int i,j;
        Coordinate square;
        numOfShipsActive=5;
        numOfShipsNotActive=0;
        isBoardActive=true;
        
        for(i=0; i < ships.length; i++)
            ships[i].setIsActive(true);
        
        for(i=0; i<10; i++)
            for(j=0; j<10; j++)
               gameBoard[i][j] = new Cell(i,j);
        
        for(i=0; i<ships.length; i++){
            square= ships[i].getFrontOfShip();
            setShipOnBoard(square.getRow(),square.getColumn(),i,ships[i].getSizeOfShip(),ships[i].getWayOfPlacement(),ships[i].getShipName());
        }   
    }

    
    /**
     * This function returns true if coordinate contains a ship,false otherwise.
     * @param point the coordinate to be checked
     * @return boolean
     */
    public boolean getContainShip(Coordinate point){
    
        return gameBoard[point.getRow()][point.getColumn()].getContainShip();
    }
    
    /**
     * This function marks a coordinate as Hit-* means a ship was hit
     * @param square the coordinate to set as Hit
     */
    public void setShipHit(Coordinate square){
        
        gameBoard[square.getRow()][square.getColumn()].setContainShip(false);
        gameBoard[square.getRow()][square.getColumn()].setMark("*");
        int numberOfShip = gameBoard[square.getRow()][square.getColumn()].getShipNum();
        ships[numberOfShip].setShipActiveParts(ships[numberOfShip].getShipActiveParts()-1);
        if(ships[numberOfShip].getShipActiveParts() == 0){
            ships[numberOfShip].setIsActive(false);
            numOfShipsActive--;
            numOfShipsNotActive++;
        }
    }
    
    /**
     * This function sets a ship on the board
     * @param row the coordinates row
     * @param col the coordinates column
     * @param shipNum the ship number
     * @param size the size of the ship
     * @param direction way of placement(Horizontal=1 ,Vertical=2)
     * @param shipName
     * @return boolean -true if succeeded,false otherwise
     */
    public boolean setShipOnBoard(int row ,int col,int shipNum, int size, int direction,String shipName){
        
        boolean ans =false;
        if(checkPlacing(row,col,shipNum,size,direction) == false){
            Coordinate c = new Coordinate(row,col);   
            ships[shipNum]=new Ship(c, size, shipName, direction);
            
            for(int i=0; i< size;i++){
                switch(direction){
                   case Ship.HORIZONTAL : gameBoard[row][col].setContainShip(true,shipNum);  
                        gameBoard[row][col].setMark("S");
                        col++;
                        break;  
                   case Ship.VERTICAL : gameBoard[row][col].setContainShip(true,shipNum);  
                        gameBoard[row][col].setMark("S");
                        row++;
                        break;
                    default:
                }
            }
            ans = true;
        }
        return ans; 
    }
    
    /**
     * This function
     * @param row the coordinates row
     * @param col the coordinates column
     * @param shipNum the ship number
     * @param sizeSize the size of the ship
     * @param direction way of placement(Horizontal=1 ,Vertical=2)
     * @return boolean-false if the placing is good,truth if it's not good(means it contains a ship)
     */
    private boolean checkPlacing(int row, int col , int shipNum,int shipSize, int direction){
        
        boolean isContainShip=false;
        int x = row;
        int y = col;
        
        for(int i = 0;(i < shipSize && !isContainShip);  i++){    
            switch(direction){
                case Ship.HORIZONTAL : if(y + shipSize <= 10){
                        isContainShip = gameBoard[row][col].getContainShip();//dwon
                        col++;                       
                        }
                        else
                             isContainShip = true;
                        break;
                 case Ship.VERTICAL : if(x + shipSize <= 10){
                        isContainShip = gameBoard[row][col].getContainShip();//right
                        row++;
                        }
                        else
                             isContainShip =true;
                        break;
                 default:
            }
        }
        return isContainShip ;
     }
    
    
    /**
     * Gets the ships array
     * @return Ship array
     */
    public Ship[] getShips() {
        return ships;
    }

    /**
     * Sets the ships array
     * @param ships a ships array
     */
    public void setShips(Ship[] ships) {
        this.ships = ships;
    }
    
    /**
     * Gets the number of active ships on the board
     * @return numOfShipsActive
     */
    public int getNumOfShipsActive() {
        return numOfShipsActive;
    }

    /**
     * Gets the number of inactive ships
     * @return numOfShipsNotActive
     */
    public int getNumOfShipsNotActive() {
        return numOfShipsNotActive;
    }
    
    /**
     * Sets the player number in the board
     * @param num player number
     */
    public void  setPlayerNumber(int num) {
         PlayerNumber=num;
    }
     
     /**
      * Gets the player number
      * @return PlayerNumber
      */
     public int getPlayerNumber() {
        return PlayerNumber;
    }
    
     /**
      * Gets the board
      * @return gameBoard
      */
     public Cell [][] GetGameBoard(){
        return gameBoard;
    }
    
    /**
     * Sets the number of active ships
     * @param numOfShipsActive number of active ships
     */
    public void setNumOfShipsActive(int numOfShipsActive) {
        this.numOfShipsActive = numOfShipsActive;
    }

    /**
     * Sets the number of inactive ships
     * @param numOfShipsNotActive number of inactive ships
     */
    public void setNumOfShipsNotActive(int numOfShipsNotActive) {
        this.numOfShipsNotActive = numOfShipsNotActive;
    }
    
     /**
     * Returns if board is active
     * @return boolean-true if board is active,false otherwise
     */
    public boolean isIsBoardActive() {
        return isBoardActive;
    }

    /**
     * Sets if the board is active
     * @param isBoardActive true-to active board.false-to deactivate board
     */
    public void setIsBoardActive(boolean isBoardActive) {
        this.isBoardActive = isBoardActive;
    }
    
}